<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="日期范围" prop="dateRange1">
        <el-date-picker
          v-model="dateFrame"
          type="daterange"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="上午时间段">
        <el-time-picker
          v-model="morningTimeFrame"
          is-range
          format="HH:mm"
          value-format="HH:mm"
          :style="{ width: '100%' }"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          range-separator="至"
        >
        </el-time-picker>
      </el-form-item>
      <el-form-item label="下午时间段">
        <el-time-picker
          v-model="afternoonTimeFrame"
          is-range
          format="HH:mm"
          value-format="HH:mm"
          :style="{ width: '100%' }"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          range-separator="至"
        >
        </el-time-picker>
      </el-form-item>
      <el-form-item label="时间粒度(分钟)" prop="unit">
        <el-input
          v-model="queryParams.unit"
          placeholder="请输入时间粒度(分钟)"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="粒度人数上限" prop="numLimit">
        <el-input
          v-model="queryParams.numLimit"
          placeholder="请输入时间片人数上限"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['doctor:free_time:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="free_timeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column :label="personReserved + '姓名'" prop="nickname" />
      <el-table-column
        label="日期范围"
        align="center"
        prop="dateRange1"
        width="180"
      >
        <template slot-scope="scope">
          <span
            >{{ parseTime(scope.row.dateRange1, "{y}-{m}-{d}") }}至{{
              parseTime(scope.row.dateRange2, "{y}-{m}-{d}")
            }}</span
          >
        </template>
      </el-table-column>
      <el-table-column label="上午时间范围" align="center" prop="morning1">
        <template slot-scope="scope">
          <span>{{ scope.row.morning1 }}-{{ scope.row.morning2 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下午时间范围" align="center" prop="afternoon1">
        <template slot-scope="scope">
          <span>{{ scope.row.afternoon1 }}-{{ scope.row.afternoon2 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="时间粒度(分钟)" align="center" prop="unit" />
      <el-table-column label="时间片人数上限" align="center" prop="numLimit" />
      <el-table-column label="去掉节假日" align="center" prop="noHoliday" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="showMore(scope.row)"
            >更多</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['doctor:free_time:edit']"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改空闲时间 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="日期范围" prop="dateRange">
          <el-date-picker
            type="daterange"
            v-model="dateRange"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            :style="{ width: '100%' }"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            range-separator="至"
            clearable
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="上午时间范围" prop="morning">
          <el-time-picker
            style="width: 100%"
            is-range
            format="HH:mm"
            value-format="HH:mm"
            v-model="morning"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            range-separator="至"
            clearable
          ></el-time-picker>
        </el-form-item>
        <el-form-item label="下午时间范围" prop="afternoon">
          <el-time-picker
            style="width: 100%"
            is-range
            format="HH:mm"
            value-format="HH:mm"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            range-separator="至"
            clearable
            v-model="afternoon"
          ></el-time-picker>
        </el-form-item>
        <el-form-item label="去除法定节假日" prop="noHoliday">
          <el-radio-group v-model="form.noHoliday" @change="change">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="粒度人数上限" prop="numLimit">
          <el-input-number
            v-model="form.numLimit"
            :min="1"
            :max="9999"
            label="描述文字"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="时间粒度(分钟)" prop="unit">
          <el-select
            v-model="form.unit"
            clearable
            @change="changeUnit"
            default-first-option
          >
            <el-option
              v-for="item in unitOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否提前提醒" prop="preRemind">
          <el-select v-model="form.preRemind" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              :disabled="item.disabled"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="preRemindContent">
          <el-input
            type="textarea"
            v-model="form.preRemindContent"
            placeholder="请输入提前提醒内容,不输入则使用系统默认"
            :autosize="{ minRows: 4, maxRows: 4 }"
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="预约成功马上发送通知" prop="sendImmediate">
          <el-select v-model="form.sendImmediate" placeholder="请选择">
            <el-option
              v-for="item in sendImmediateOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              :disabled="item.disabled"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="sendImmediateContent">
          <el-input
            type="textarea"
            clearable
            v-model="form.sendImmediateContent"
            placeholder="请输入预约成功发送给家长的内容,不输入则使用系统默认"
            :autosize="{ minRows: 4, maxRows: 4 }"
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="预约说明" prop="remark">
          <el-input
            type="textarea"
            clearable
            v-model="form.remark"
            placeholder="请输入备注,不输入则使用系统默认"
            :autosize="{ minRows: 4, maxRows: 4 }"
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item
          size="large"
          style="display: flex; justify-content: center"
        >
          <el-button type="primary" @click="submitForm">提交</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
listDoctors
    <el-dialog
      title="更多"
      :visible.sync="showMoreDialog"
      width="500px"
      append-to-body
    >
      <el-table :data="detail">
        <el-table-column label="是否提前提醒" align="center" prop="preRemind" />
        <el-table-column
          label="提前提醒时间(小时)"
          align="center"
          prop="preRemindTime"
        />
        <el-table-column
          label="提前提醒内容"
          align="center"
          prop="preRemindContent"
        />
        <el-table-column
          label="预约成功是否立即发送通知"
          align="center"
          prop="sendImmediate"
        />
        <el-table-column
          label="预约成功立即发送的内容"
          align="center"
          prop="sendImmediateContent"
        />
        <el-table-column label="其它说明" align="center" prop="remark" />
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import {
  listByUserId,
  getFree_time,
  delFree_time,
  updateFree_time,
} from "@/api/doctor/free_time";
import { listDoctors } from "@/api/system/user";
export default {
  name: "DoctorTimeList",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      dateFrame: "",
      morningTimeFrame: "",
      afternoonTimeFrame: "",
      // 子表选中数据
      checkedTimeSlices: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      showMoreDialog: false,
      // 总条数
      total: 0,
      // 空闲时间(用于切片的)表格数据
      free_timeList: [],
      // 时间片表格数据
      title: "",
      detail: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        dateRange1: null,
        dateRange2: null,
        morning1: null,
        morning2: null,
        afternoon1: null,
        afternoon2: null,
        noHoliday: null,
        numLimit: null,
        unit: null,
        preRemind: null,
        preRemindTime: null,
        preRemindContent: null,
        sendImmediate: null,
        sendImmediateContent: null,
      },
      // 表单参数
      form: {
        noHoliday: 1,
        numLimit: 1,
        unit: 30,
        preRemind: "是",
        preRemindTime: 20,
        preRemindContent: "",
        sendImmediate: "是",
        sendImmediateContent: "",
        remark: "",
      },
      personReserved: "家长",
      dateRange: ["", ""],
      morning: ["07:00", "12:00"],
      afternoon: ["14:00", "17:00"],
      unitOptions: [
        {
          value: "10",
          label: "10",
        },
        {
          value: "15",
          label: "15",
        },
        {
          value: "20",
          label: "20",
        },
        {
          value: "30",
          label: "30",
        },
        {
          value: "60",
          label: "60",
        },
        {
          value: "120",
          label: "120",
        },
      ],
      options: [
        {
          value: "是",
          label: "是",
        },
        {
          value: "否",
          label: "否",
        },
      ],
      sendImmediateOptions: [
        {
          value: "是",
          label: "是",
        },
        {
          value: "否",
          label: "否",
        },
      ],
      preRemindTimeOptions: [
        {
          value: "1",
          label: "1",
        },
        {
          value: "2",
          label: "2",
        },
        {
          value: "3",
          label: "3",
        },
        {
          value: "4",
          label: "4",
        },
        {
          value: "5",
          label: "5",
        },
        {
          value: "10",
          label: "10",
        },
        {
          value: "20",
          label: "20",
        },
        {
          value: "30",
          label: "30",
        },
        {
          value: "40",
          label: "40",
        },
      ],
      userList: [],
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
    this.getConfigKey("sys.personReserved").then((response) => {
      this.personReserved = response.msg;
    });
  },
  methods: {
    /** 查询空闲时间(用于切片的)列表 */
    async getList() {
      this.loading = true;
      if (this.dateFrame) {
        this.queryParams.dateRange1 = this.dateFrame[0];
        this.queryParams.dateRange2 = this.dateFrame[1];
      }
      if (this.morningTimeFrame) {
        this.queryParams.morning1 = this.morningTimeFrame[0];
        this.queryParams.morning2 = this.morningTimeFrame[1];
      }
      if (this.afternoonTimeFrame) {
        this.queryParams.afternoon1 = this.afternoonTimeFrame[0];
        this.queryParams.afternoon2 = this.afternoonTimeFrame[1];
      }
      await listByUserId(this.queryParams).then((response) => {
        this.free_timeList = response.rows;
        this.total = response.total;
      });
      this.loading = false;
    },
    showMore(row) {
      this.detail = [];
      this.detail.push(row);
      this.showMoreDialog = true;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 20;
      this.getList();
    },
    /** 重置按钮操作 */
    async resetQuery() {
      await this.resetForm("queryForm");
      this.$set(this, "dateFrame", "");
      this.$set(this, "morningTimeFrame", "");
      this.$set(this, "afternoonTimeFrame", "");
      this.queryParams.dateRange1 = "";
      this.queryParams.dateRange2 = "";
      this.queryParams.morning1 = "";
      this.queryParams.morning2 = "";
      this.queryParams.afternoon1 = "";
      this.queryParams.afternoon2 = "";
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    async handleUpdate(row) {
      const id = row.id;
      this.title = "修改空闲时间";
      await getFree_time(id).then((response) => {
        this.form = response.data;
        this.$set(this, "dateRange", [
          this.form.dateRange1,
          this.form.dateRange2,
        ]);
        this.$set(this, "morning", [this.form.morning1, this.form.morning2]);
        this.$set(this, "afternoon", [
          this.form.afternoon1,
          this.form.afternoon2,
        ]);
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.dateRange && this.dateRange.length > 1) {
            this.form.dateRange1 = this.dateRange[0];
            this.form.dateRange2 = this.dateRange[1];
          }
          if (this.morning && this.morning.length > 1) {
            this.form.morning1 = this.morning[0];
            this.form.morning2 = this.morning[1];
            var hh = this.morning[1].substring(0, 2);
            var mm = this.morning[1].substring(3, 5);
            if (hh > 12 || (hh == 12 && mm > 0)) {
              return this.$message.warning("上午时间小于12:00");
            }
          }
          if (this.afternoon.length > 1) {
            this.form.afternoon1 = this.afternoon[0];
            this.form.afternoon2 = this.afternoon[1];
            var h = this.afternoon[0].substring(0, 2);
            var m = this.afternoon[0].substring(3, 5);
            if (h < 12) {
              return this.$message.warning("下午时间大于12:00");
            }
          }
          this.form.preRemind =
            this.form.preRemind == "是" || this.form.preRemind == 1 ? 1 : 0;
          this.form.sendImmediate =
            this.form.sendImmediate == "是" || this.form.sendImmediate == 1
              ? 1
              : 0;
          if (this.form.id) {
            updateFree_time(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.$message.warning("时间段id为空");
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除该空闲时间？")
        .then(function () {
          return delFree_time(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出全部按钮操作 */
    handleExport() {
      this.download(
        "doctor/free_time/export",
        {
          ...this.queryParams,
        },
        `free_time_${new Date().getTime()}.xlsx`
      );
    },
    changeUnit() {
      console.log(this.form.unit);
    },
    change() {
      console.log(this.form.noHoliday);
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 20;
      this.getList();
    },
  },
};
</script>